<?php
namespace app\api\controller;
use think\Session;
use think\Db;
use app\api\controller\Base;

class Candidate extends  Base
{
	public function _initialize()
	{
		parent::_initialize();  //调用父类构造方法 
		$check = $this->state;
	    if($check == -1)
	    {
	    	 $err['code'] = -1;
             $err['message'] = '尚未登录';
             echo json_encode($err,JSON_UNESCAPED_UNICODE);die;
	    }

	}

	/**
	*获取候选人名单信息
	*/
	public function lst()
	{
		if(request()->isPost())
		{
			//根据前台传的标识判断 先进个人 or 师德标兵
			$post = file_get_contents("php://input");
			$data = json_decode($post,true);

			//标识
			$sign = $data['sign'];

			//获取数据
			if(!isset($sign) || empty($sign))
			{
				$info['code'] = -1;
				$info['message'] 	= '非法获取';
				return json_encode($info,JSON_UNESCAPED_UNICODE);
			}else
			{
				//判断此用户是否已经投过此类型的票
				/*$account = Session::get('account','think');
				$code = db('voter',[],false)->where('account',$account)->column('status');

				if(in_array($sign,$code))
				{
					$info['code'] = -1;
					$info['message'] 	= '抱歉，您的机会已用完!';
					return json_encode($info,JSON_UNESCAPED_UNICODE);
				}*/

				if($sign == 'personal'  ) //先进个人  
				{
					$res = Db::query("select id,image,name,story,unit from tp_candidate");
					$info['code'] = 0;
					$info['data'] 	= $res;
					return json_encode($info,JSON_UNESCAPED_UNICODE);
				}elseif($sign == 'pacesetter') //师德标兵
				{
					$res = Db::query("select id,image,name,story,unit from tp_candidate where sign=1 ");
					$info['code'] = 0;
					$info['data'] 	= $res;
					return json_encode($info,JSON_UNESCAPED_UNICODE);
				}else
				{
					$info['code'] = -1;
					$info['message'] 	= '标识出错';
					return json_encode($info,JSON_UNESCAPED_UNICODE);
				}
			}
		}else
		{
			$info['code'] = -1;
        	$info['message'] = '非法方式传输';
        	return json_encode($info,JSON_UNESCAPED_UNICODE);
		}
		
	}
}